// @ 516
// _(:з」∠)_

define([ "layDate" ], function( laydate ) {
    'use strict';

    const TimeLine = {};

    TimeLine["validMain"] = {
        timeInvt: 24 * 60 * 60 * 1000,
        init: function( param ) {
            let _this = this;

            let timeLent = 1,
                startIndex = 0,
                defaultValue = "";

            if ( 
                param.startTime && param.endTime
                && param.startTime != "" && param.endTime != ""
            ) {
                let invt = _this.timeInvt;
                startIndex = _this.formDate({ type: "date", date: param.startTime }) - 1;
                if ( param.type == "date" ) invt = _this.timeInvt / 24;
                if ( param.type == "month" ) invt = _this.timeInvt * 1;

                param.type = "custom";
                timeLent = ( new Date( param.endTime ).getTime() - new Date( param.startTime ).getTime() ) / invt + 1;
            } else if ( param.type == "customx" ) {
                timeLent = param.customItems.length;
            } else {
                let thisTime = new Date(),
                    _year = thisTime.getFullYear(),
                    _month = thisTime.getMonth() + 1,
                    _date = thisTime.getDate();

                if ( param.type == "date" ) {
                    timeLent = 24;
                    defaultValue = _year+"-"+_month+"-"+_date;
                }
                if ( param.type == "month" ) {
                    timeLent = new Date( _year, _month, 0 ).getDate();
                    defaultValue = _year+"-"+_month;
                }
                if ( param.type == "year" ) {
                    timeLent = 12;
                    defaultValue = _year;
                }
            }

            param["timeLent"] = timeLent;
            param["startIndex"] = startIndex;
            param["defaultValue"] = defaultValue;
            _this.render( param );
        },
        render: function( param ) {
            let _this = this,
                main = param.elmt;
                
            if ( !main ) main = document.body.querySelector(".main");

            let lineItemHtml = ``;
            for ( let x = 0; x < param.timeLent; x++ ) {
                let thisDay = "", thisTime = "",
                    _date  = new Date(),
                    _year  = _date.getFullYear(),
                    _month = _date.getMonth() + 1,
                    _day   = _date.getDate();

                param.startIndex += 1;
                if ( param.type === "custom" ) {
                    let date_ = new Date( new Date( param.startTime ).getTime() + (_this.timeInvt * x) );
                    thisDay = date_.getDate();
                    if ( thisDay < 10 ) thisDay = "0" + thisDay;
                    thisTime = formatTime( date_, "day" );
                } else if ( param.type === "month" ) {
                    thisDay = param.startIndex;
                    if ( thisDay < 10 ) thisDay = "0" + thisDay;
                    thisTime = _year+"-"+_month+"-"+thisDay;
                } else if ( param.type === "date" ) {
                    thisDay = _day;
                    if ( thisDay < 10 ) thisDay = "0" + thisDay;
                    thisTime = _year+"-"+_month+"-"+thisDay;
                }
                
                if ( param.type != "customx" ) {
                    lineItemHtml += `<li class="line_item" index="${ x }" time="${ thisTime }" title="${ thisTime }">
                                                    <h></h> <span>${ thisDay }</span>
                                                </li>`;
                } else {
                    lineItemHtml += `<li class="line_item" index="${ x }" time="${ param.customItems[x].value }" title="${ param.customItems[x].value }">
                                                    <h></h> <span>${ param.customItems[x].value.split("-")[2] }</span>
                                                </li>`;
                }
            }

            let mainHtml = `<div class="timeLine">
                                <div class="line_tools">
                                    <div id="input_date" title="时间选择"></div>
                                    <button type="button" name="prev"></button>
                                    <button type="button" name="play"></button>
                                    <button type="button" name="next"></button>
                                </div>
                                <div class="line_main">
                                    <ul class="line_list">
                                        ${ lineItemHtml }
                                    </ul>
                                </div>
                            </div>`;
            
            _this.remove();
            main.insertAdjacentHTML( "beforeEnd", mainHtml );

            if ( param.type != "custom" && param.type != "customx" ) {
                laydate.render({
                    elem: '#input_date'
                    ,type: param.type
                    ,value: param.defaultValue
                    ,done: function( value, date ){
                        let timeLent = new Date( date.year, date.month, 0 );

                        _this.upDateItem({
                            elmt: document.querySelector(".line_list"),
                            date: new Date( value ),
                            timeLent: timeLent.getDate()
                        });
                    }
                });
            } else {
                document.querySelector("#input_date").remove();
            }

            param["elmt"] = main.querySelector(".timeLine");
            param["elmt"].style.left = ( main.clientWidth - param["elmt"].clientWidth ) / 2 +'px';
            _this.clickEvent( param );
        },
        clickEvent: function( param ) {
            let _this = this,
                interval = 10000;

            if ( param.interval ) interval = param.interval;

            // 触发点击事件
            param.elmt.querySelector(".line_list").addEventListener( "click", function(e) {
                let thisCurrt = null;

                if ( e.target.nodeName === "LI" ) {
                    thisCurrt = e.target;
                } else if ( e.target.parentNode.nodeName === "LI" ) {
                    thisCurrt = e.target.parentNode;
                }

                thisCurrt.removeAllClass("select");
                thisCurrt.addClass("select");

                e.stopPropagation();
            }, false);

            // 时间轴工具
            let lineTools = param.elmt.querySelectorAll(".line_tools button");
            for ( let x = 0; x < lineTools.length; x++ ) {
                lineTools[x].addEventListener( "click", function(e) {
                    let lineItems = param.elmt.querySelectorAll(".line_item"),
                        lineItemx = param.elmt.querySelector(".line_item.select");

                    if ( lineTools[x].getAttribute("name") == "prev" ) {
                        if ( lineItemx != lineItems[0] ) {
                            lineItemx.previousElementSibling.click();
                        }
                    } else if ( lineTools[x].getAttribute("name") == "next" ) {
                        if ( lineItemx != lineItems[ lineItems.length - 1 ] ) {
                            lineItemx.nextElementSibling.click();
                        }
                    } else if ( lineTools[x].getAttribute("name") == "play" ) {
                        if ( lineItemx != lineItems[ lineItems.length - 1 ] ) {
                            lineTools[x].setAttribute("name", "pause");

                            let autoPlay = setInterval(function() {
                                let selectItem = param.elmt.querySelector(".line_item.select");
                                if ( selectItem.nextElementSibling && lineTools[x].getAttribute("name") == "pause" ) {
                                    selectItem.nextElementSibling.click();
                                } else {
                                    lineTools[x].setAttribute("name", "play");
                                    clearInterval('autoPlay');
                                }
                            }, interval);
                        }
                    } else if ( lineTools[x].getAttribute("name") == "pause" ) {
                        lineTools[x].setAttribute("name", "play");
                    }
                }, false);
            }
        },
        upDateItem: function( param ) {
            let _this = this,
                lineItemHtml = ``;

            for ( let x = 0; x < param.timeLent; x++ ) {
                let _date  = param.date,
                    _year  = _date.getFullYear(),
                    _month = _date.getMonth() + 1;

                let thisDay = x+1;
                if ( thisDay < 10 ) thisDay = "0" + thisDay;
                if ( _month < 10 ) _month = "0" + _month;
                let thisTime = _year+"-"+_month+"-"+thisDay;
                
                lineItemHtml += `<li class="line_item" index="${ x }" time="${ thisTime }">
                                                <h title="${ thisTime }"></h> <span>${ x+1 }</span>
                                            </li>`;
            }

            param.elmt.textContent = "";
            param.elmt.insertAdjacentHTML( "beforeEnd", lineItemHtml );
            param.elmt.style.left = ( document.body.querySelector(".main").clientWidth - param.elmt.clientWidth ) / 2 +'px';

            let _day = new Date().getDate() - 2;
            document.querySelectorAll(".line_item")[_day].click();
        },
        formDate: function( param ) {
            let thisDate = new Date();
            if ( param.date && param.date != "" ) {
                thisDate = new Date( param.date );
            }

            let _year = thisDate.getFullYear(),
                _month = thisDate.getMonth() + 1,
                _date   = thisDate.getDate(),
                _hour  = thisDate.getHours();

            if ( param.type == "hour" ) {
                return Number( _hour );
            } else if ( param.type == "date" ) {
                return Number( _date );
            } else if ( param.type == "month" ) {
                return Number( _month );
            } else if ( param.type == "year" ) {
                return Number( _year );
            }
        },
        remove: function() {
            let _this = this,
                axis = document.querySelectorAll(".timeLine");

            if ( axis.length > 0 ) {
                for ( let x = axis.length - 1; x >= 0; x-- ) {
                    axis[x].remove();
                }
            }
        }
    }

    window.TimeLine = TimeLine;

    return TimeLine;
});